<?php
class UserIdentity extends CUserIdentity {
    
    const ERROR_ACCOUNT_BANNED = 3;
    const ERROR_ACCOUNT_BLOCKED = 4;
    
    private $_id;
    public $unblockDate;
    
    public function authenticate() {
        $record = Yii::app()->db->createCommand()->select('u.*, g.*')->from('users u')->leftJoin('users_groups g', 'g.groupId=u.groupId')->where('`u`.`password`="'.$this->password.'" and `u`.`login`="'.$this->username.'"')->queryRow();
        if(empty($record)) $this->errorCode = self::ERROR_USERNAME_INVALID;
        else if($record['isBanned']) $this->errorCode = self::ERROR_ACCOUNT_BANNED;
        else if($record['unblockDate'] > time()) {
            $this->unblockDate = $record->unblockDate;
            $this->errorCode = self::ERROR_ACCOUNT_BLOCKED;
        } else {
            $this->_id=$record['userid'];
            $this->setUserStates($record);
            Yii::app()->db->createCommand()->update('users', array('logDate'=>time()), 'userid='.$this->getId());
            $this->errorCode=self::ERROR_NONE;
        }
        return !$this->errorCode;
    }
    
    public function getId() {
        return $this->_id;
    }
    
    private function setUserStates($record) {
        $this->setState('login', $record['login']);
        $this->setState('email', $record['email']);
        $this->setState('weight', $record['weight']);
        $this->setState('site', $record['site']);
        $this->setState('twitter', $record['twitter']);
        $this->setState('skype', $record['skype']);
        $this->setState('regDate', $record['regDate']);
        $this->setState('avatar', $record['avatar']);
        $this->setState('groupId', $record['groupId']);
        $this->setState('descr', $record['descr']);
        $this->setState('userid', $record['userid']);
        $this->setState('role', $record['role']);
        $this->setState('displayName', $record['displayName']);
        $this->setState('logDate', time());
        # role options:
        $this->setState('seeAnonim', $record['seeAnonim']);         # видит кто оставил анонимный отзыв
        $this->setState('editReview', $record['editReview']);       # может редакторировать отзывы
        $this->setState('editComment', $record['editComment']);     # может редактирова каменты к отзывам
        $this->setState('moderForum', $record['moderForum']);       # может модерировать минифорум
        $this->setState('moderPartner', $record['moderPartner']);   # может модерировать партнерки и категории
        $this->setState('moderPost', $record['moderPost']);         # может править сообщения к темам на форуме
        $this->setState('moderUser', $record['moderUser']);         # может банить юзеров
        $this->setState('moderNews', $record['moderNews']);         # может править новости
        $this->setState('moderBlog', $record['moderBlog']);         # может править статьи из блога
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
/*    public function authenticate($model) {
        if($model) {
            $record = User::model()->findByAttributes(array('u_identity'=>$loginzaModel->identity, 'u_provider'=>$loginzaModel->provider));
        } else {
            $record = User::model()->findByAttributes(array('password' => $this->password, 'login' => $this->username));
        }
        if($record === null) {
            if($model) {
                $record = new User();
                $record->email = $model->email;
                $record->login = $model->full_name;
                $record->u_identity = $model->identity;
                $record->u_provider = $model->provider;
                $record->save(false);
            } else $this->errorCode = self::ERROR_USERNAME_INVALID;
        }
        else if($record->isBanned) {
            $this->errorCode = self::ERROR_ACCOUNT_BANNED;
        }
        else if($record->unblockDate > time()) {
            $this->unblockDate = $record->unblockDate;
            $this->errorCode = self::ERROR_ACCOUNT_BLOCKED;
        }
        
        if(!$this->errorCode && $record !== null) {
            $this->_id=$record->userid;
            $this->setUserStates($record);
            User::model()->updateByPk($record->userid, array('logDate'=>time()));
            $this->errorCode=self::ERROR_NONE;
        }
        return !$this->errorCode;
    }
    
    */
}